extends Control

#节点加载---start
@onready var func_card_shop = $ShopFuncCard/FuncCardShop
@onready var func_card_player = $PlayerFuncCard/FuncCardPlayer
@onready var func_card_carry = $PlayerReadyCard/FuncCardPlayer
@onready var player_money = $PlayerMoney
@onready var buy_detail = $BuyDetail
@onready var player_props = $PlayerProps
#节点加载---end

#实例化class类---start
var shopFunctionCardDeck = ShopDetail.functionCardDeck
var randomFuncCardArr = []
#实例化class类---end

func _ready() -> void:
	randomFuncCard()
	showPlayerMoeny()
	showShopRandomFuncCard()
	showPlayerFuncCard()
	showProps(PlayerDetail.currentCarryFunctionProps,player_props)

#清空容器
func clear_container(container: Node):
	for c in container.get_children():
		container.remove_child(c)
		c.queue_free()

#初始化随机家Shop功能牌
func randomFuncCard():
	var random_count = randi() % 3  # 随机决定选择 0,1,2 个元素
	match random_count:
		2:
			# 若数组不够2个，直接返回空
			if shopFunctionCardDeck.size() < 2:
				return []
			# 获取两个不同的随机索引
			var index1 = randi() % shopFunctionCardDeck.size()
			var index2 = randi() % shopFunctionCardDeck.size()
			# 确保 index1 和 index2 不相同
			while index1 == index2:
				index2 = randi() % shopFunctionCardDeck.size()
			randomFuncCardArr = [shopFunctionCardDeck[index1], shopFunctionCardDeck[index2]]
		
		1,0:
			# 若数组不够1个，直接返回空
			if shopFunctionCardDeck.size() < 1:
				return []
			# 随机选择一个元素
			var index = randi() % shopFunctionCardDeck.size()
			randomFuncCardArr = [shopFunctionCardDeck[index]]
	

#随机展示Shop功能牌
func showShopRandomFuncCard():
	showFuncCards(randomFuncCardArr,func_card_shop)

#展示玩家功能牌
func showPlayerFuncCard():
	showFuncCards(PlayerDetail.currentFunctionCardDeck,func_card_player)
	showFuncCards(PlayerDetail.currentCarryFunctionCardDeck,func_card_carry)

#展示功能牌
func showFuncCards(funcCards,container):
	for item in funcCards:
		# 拼接功能牌场景路径
		var scenePath = "res://GameScenes/FunctionCardScene/" + item + "/" + item + ".tscn"
		# 加载场景并确保返回的是一个 PackedScene
		var funcCardScene = load(scenePath)
		# 检查是否是 PackedScene 类型
		if funcCardScene and funcCardScene is PackedScene:  
			# 创建一个 Container 节点作为容器
			var cardContainer = HBoxContainer.new()
			cardContainer.custom_minimum_size = Vector2(100,100)
			# 实例化场景
			var funcCardInstance = funcCardScene.instantiate()
			if funcCardInstance:
				 # 将功能牌添加到 Control 容器中
				cardContainer.add_child(funcCardInstance)
				if container == func_card_shop:
					# 创建购买按钮
					var button = Button.new()
					button.custom_minimum_size = Vector2(0,0)
					button.text = "¥" + "\n" + str(funcCardInstance.cost)
					button.connect("pressed", Callable(self, "_onBuyPressed").bind(funcCardInstance))
					cardContainer.add_child(button)
				elif container == func_card_player:
					var vboxContainer = VBoxContainer.new()
					var button2 = Button.new()
					button2.custom_minimum_size = Vector2(20,20)
					button2.text = "携" + "\n" + "带"
					button2.connect("pressed", Callable(self, "_onCarryPressed").bind(funcCardInstance))
					vboxContainer.add_child(button2)
					
					# 创建删除按钮
					var button = Button.new()
					button.custom_minimum_size = Vector2(20,20)
					button.text = "移" + "\n" + "除"
					button.connect("pressed", Callable(self, "_onDeletePressed").bind(funcCardInstance))
					vboxContainer.add_child(button)
					
					cardContainer.add_child(vboxContainer)
				elif container == func_card_carry:
					var vboxContainer = VBoxContainer.new()
					var button2 = Button.new()
					button2.custom_minimum_size = Vector2(20,20)
					button2.text = "放" + "\n" + "回"
					button2.connect("pressed", Callable(self, "_onBackPressed").bind(funcCardInstance))
					vboxContainer.add_child(button2)
					
					# 创建删除按钮
					#var button = Button.new()
					#button.custom_minimum_size = Vector2(20,20)
					#button.text = "移" + "\n" + "除"
					#button.connect("pressed", Callable(self, "_onDeletePressed").bind(funcCardInstance))
					#vboxContainer.add_child(button)
					
					cardContainer.add_child(vboxContainer)
				container.add_child(cardContainer)
			else:
				print("===未找到该资源===")
		else:
			print("===未找到该资源===")

#展示玩家金币
func showPlayerMoeny():
	player_money.text = "拥有¥" + str(PlayerDetail.playerMoney)

#购买按钮
func _onBuyPressed(funcCardInstance):
	var cost = funcCardInstance.cost
	var funcCardId = funcCardInstance.card_id
	buy_detail.visible = true
	if cost > PlayerDetail.playerMoney:
		buy_detail.text = "购买失败～"
	else:
		buy_detail.text = "购买成功！"
		PlayerDetail.playerMoney -= cost #更新玩家金币
		showPlayerMoeny() #更新玩家金币文本
		PlayerDetail.currentFunctionCardDeck.append(funcCardId) #更新玩家功能卡数组
		shopFunctionCardDeck.erase(funcCardId) #更新商店功能卡数组
		randomFuncCardArr.erase(funcCardId)
		clear_container(func_card_player) #清空区域后重新渲染
		clear_container(func_card_shop)
		showFuncCards(PlayerDetail.currentFunctionCardDeck,func_card_player) #重新渲染玩家功能卡牌区域
		showFuncCards(randomFuncCardArr,func_card_shop) #重新渲染商店功能卡牌区域
	await get_tree().create_timer(1).timeout  # 等待
	buy_detail.visible = false

#删除按钮
func _onDeletePressed(funcCardInstance):
	var funcCardId = funcCardInstance.card_id
	clear_container(func_card_player) #清空区域后重新渲染
	clear_container(func_card_carry) #清空区域后重新渲染
	PlayerDetail.currentFunctionCardDeck.erase(funcCardId) #更新玩家功能卡数组
	PlayerDetail.currentCarryFunctionCardDeck.erase(funcCardId) #更新玩家携带功能卡数组
	showFuncCards(PlayerDetail.currentFunctionCardDeck,func_card_player) #重新渲染玩家功能卡牌区域
	showFuncCards(PlayerDetail.currentCarryFunctionCardDeck,func_card_carry) #重新渲染玩家功能卡牌区域

#携带按钮
func _onCarryPressed(funcCardInstance):
	var funcCardId = funcCardInstance.card_id
	PlayerDetail.currentCarryFunctionCardDeck.append(funcCardId) #更新玩家携带功能卡数组
	PlayerDetail.currentFunctionCardDeck.erase(funcCardId) #更新玩家功能卡数组
	clear_container(func_card_player) #清空区域后重新渲染
	clear_container(func_card_carry) #清空区域后重新渲染
	showFuncCards(PlayerDetail.currentFunctionCardDeck,func_card_player) #重新渲染玩家功能卡牌区域
	showFuncCards(PlayerDetail.currentCarryFunctionCardDeck,func_card_carry) #重新渲染玩家功能卡牌区域
	
#放回按钮
func _onBackPressed(funcCardInstance):
	var funcCardId = funcCardInstance.card_id
	PlayerDetail.currentCarryFunctionCardDeck.erase(funcCardId) #更新玩家携带功能卡数组
	PlayerDetail.currentFunctionCardDeck.append(funcCardId) #更新玩家功能卡数组
	clear_container(func_card_player) #清空区域后重新渲染
	clear_container(func_card_carry) #清空区域后重新渲染
	showFuncCards(PlayerDetail.currentFunctionCardDeck,func_card_player) #重新渲染玩家功能卡牌区域
	showFuncCards(PlayerDetail.currentCarryFunctionCardDeck,func_card_carry) #重新渲染玩家功能卡牌区域

#展示道具
func showProps(propArr,container):
	for item in propArr:
		# 拼接道具场景路径
		var scenePath = "res://GameScenes/FunctionPropScene/" + item + "/" + item + ".tscn"
		# 加载场景并确保返回的是一个 PackedScene
		var propScene = load(scenePath)
		# 检查是否是 PackedScene 类型
		if propScene and propScene is PackedScene:  
			# 实例化场景
			var propSceneInstance = propScene.instantiate()
			if propSceneInstance:
				if container == player_props:
					container.add_child(propSceneInstance)
			else:
				print("===未找到该资源===")
		else:
			print("===未找到该资源===")
